package com.jianyun.jyzs.http;

import android.content.Context;
import android.util.Log;


import com.jianyun.jyzs.bean.CrmFunnelList;
import com.jianyun.jyzs.bean.EnNatureResponse;
import com.jianyun.jyzs.bean.IndustryResponse;
import com.jianyun.jyzs.dao.CrmFunnelDao;
import com.jianyun.jyzs.dao.EnNatureDao;
import com.jianyun.jyzs.dao.IndustryDao;

import java.util.HashMap;
import java.util.List;

import com.jianyun.jyzs.utils.RceLog;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by mlli on 2017/8/29.
 */

public class CrmHelper {
    private Context context;

    public CrmHelper(Context context) {
        this.context = context;
    }

    //下载相关数据到本地
    public void initCrmInfo(final String enterpriseCode) {
        final CrmFunnelDao funnelDao = new CrmFunnelDao(context);
        final EnNatureDao natureDao = new EnNatureDao(context);
        try {
            Api api2 = RetrofitHelper.builderErpRoot().create(Api.class);
            HashMap<String, String> map = new HashMap<>();
            map.put("method", "crm_qyxz");
            //下载漏斗状态
            api2.getNewSomeFunnel("crm_ldjd", enterpriseCode,"jyzs","Android","1.0.2")
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new CustomCallback<CrmFunnelList>() {
                        @Override
                        protected void onResponse(CrmFunnelList response) {
                            Log.i("test", "漏斗：" + response);
                            if (response.isResult()) {
                                funnelDao.clear(enterpriseCode);
                                List<CrmFunnelList.FunnelBean> list = response.getData();
                                for (CrmFunnelList.FunnelBean el : list
                                        ) {
                                    funnelDao.insertFunnel(el, enterpriseCode);
                                }
                            }

                        }

                        @Override
                        protected void onException(String msg) {

                        }
                    });

            //下载客户性质
            api2.getNewEntNature(map)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(Schedulers.io())
                    .subscribe(new CustomCallback<EnNatureResponse>() {
                        @Override
                        protected void onResponse(EnNatureResponse response) {
                            if (response.isResult()) {
                                natureDao.clear(enterpriseCode);
                                List<EnNatureResponse.EnNatureBean> list = response.getData();
                                for (EnNatureResponse.EnNatureBean el : list
                                        ) {
                                    natureDao.insert(el, enterpriseCode);
                                }
                            }


                        }

                        @Override
                        protected void onException(String msg) {

                        }
                    });

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
